import {
    baseUrl
} from './config'

export default function request(url, methodType, data, callback) {
    let fullUrl = `${baseUrl}${url}`
    let token = wx.getStorageSync('token') ? wx.getStorageSync('token') : ''
    wx.showLoading({
        title: "加载中"
    });
    return new Promise((resolve, reject) => {
        wx.request({
            url: fullUrl,
            method: methodType,
            data,
            header: {
                'content-type': "application/x-www-form-urlencoded;charset=utf-8",
                'access_token': token,
            },
            success: (res) => {
                if (res.statusCode == 200) {
                    if (callback) {
                        callback(res.data);
                        return
                    }
                    if (res.data.result == 1) {
                        resolve(res.data);
                    } else {
                        wx.showToast({
                            title: res.data.message,
                            icon: 'none',
                            duration: 3000
                        })
                        reject(res.data.message)
                    }
                } else if (res.statusCode == 403) {
                    wx.login({
                        success(res) {
                            if (res.code) {
                                let datas = {
                                    js_code: res.code,
                                }
                                wx.request({
                                    url: `${baseUrl}/charge-api/auth/ma/token`,
                                    method: "POST",
                                    data: datas,
                                    header: {
                                        'content-type': "application/x-www-form-urlencoded;charset=utf-8"
                                    },
                                    success: (res) => {
                                        if (res.data.result == 1) {
                                            wx.setStorageSync('token', res.data.data.access_token);
                                            request(url, methodType, data, resolve);
                                        } else {
                                            wx.navigateTo({
                                                url: '/pages/login/login',
                                            })
                                        }
                                    },
                                    fail: () => {
                                        wx.showToast({
                                            title: '接口请求错误',
                                            icon: 'none',
                                            duration: 3000
                                        })
                                        reject('接口请求错误')
                                    }
                                })
                            } else {
                                console.log('登录失败！' + res.errMsg)
                            }
                        }
                    })
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            fail: () => {
                wx.showToast({
                    title: '接口请求错误',
                    icon: 'none',
                    duration: 3000
                })
                reject('接口请求错误')
            },
            complete: () => {
                wx.hideLoading()
            }
        })
    })
}